package com.hjb.service.controller.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hjb.service.cms.CmsArticleDataService;
import com.hjb.service.cms.CmsArticleService;
import com.hjb.service.common.CmsContentService;
import com.hjb.service.config.properties.WxProperties;
import com.hjb.service.domain.cmsarticle.CmsArticle;
import com.hjb.service.domain.cmsarticle.CmsArticleExample;
import com.hjb.service.domain.cmsarticledata.CmsArticleData;
import com.hjb.service.domain.cmsarticledata.CmsArticleDataExample;
import com.hjb.service.utils.DateUtil;
import com.hjb.service.utils.MapUtil;

/**
 * 公告 Controller
 * @author zhangxl
 * @date 2017-07-20
 * @version 1.0
 */
@RestController
@RequestMapping("/common")
public class CmsContentController {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private CmsContentService contentService;
	
	@Autowired
	private CmsArticleService cmsArticleService;
	
	@Autowired
	private CmsArticleDataService cmsArticleDataService;

	@Autowired
	private WxProperties wxProperties;
	
	/**
	 * 公告列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/content/list")
	@ResponseBody
	public Map<String, Object> contentList(HttpServletRequest request){
		/*Map<String,Object> dataMap = new HashMap<String, Object>();
		Map<String,Object> resMap =MapUtil.getRetMap();
		try {
			String pageNo = request.getParameter("page_no");
			String pageSize = request.getParameter("page_size");
			if (StringUtils.isBlank(pageNo)) {
				pageNo = "0";
			}
			if(StringUtils.isBlank(pageSize)){
				pageSize = "10";
			}
			
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("pageNo", pageNo);
			params.put("pageSize", pageSize);
			
			//公告列表
			List<Map<String,Object>> noticeList = contentService.getContentList(params);
			
			dataMap.put("noticeList", noticeList);
			resMap.put("data", dataMap);
			
		} catch (Exception e) {
			log.info(e.getMessage());
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");	
		}
		
		return resMap;*/
		Map<String, Object> result = MapUtil.getRetMap();
		Map<String, Object> data = new HashMap<String, Object>();
		result.put("data", data);
		
		int currentPage = 1;
		int pageSize = 10;
		String type = "3";
		String subjectName = "网站公告";
//		String currentPageStr = request.getParameter("currentPage");
//		String pageSizeStr = request.getParameter("pageSize");
		String currentPageStr = request.getParameter("page_no");
		String pageSizeStr = request.getParameter("page_size");
		String typeStr = request.getParameter("type");
		try {
			if (StringUtils.isNotBlank(currentPageStr)) {
				currentPage = Integer.parseInt(currentPageStr);
			}
			if (StringUtils.isNotBlank(pageSizeStr)) {
				pageSize = Integer.parseInt(pageSizeStr);
			}
			if (StringUtils.isNotBlank(typeStr)) {
				type = typeStr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		if (pageSize < 0) {
			pageSize = 10;
		}
		if ("3".equals(type)) {
			subjectName = "网站公告";
		}else if ("4".equals(type)) {
			subjectName = "行业动态";
		}else if ("5".equals(type)) {
			subjectName = "帮助中心";
		}
		
		List<Object> list = new ArrayList<Object>();
		Page<CmsArticle> page = PageHelper.startPage(currentPage, pageSize, true);
		CmsArticleExample cmsArticleExample = new CmsArticleExample();
		cmsArticleExample.createCriteria().andCategoryIdEqualTo(type).andDelFlagEqualTo("0")/*.andRemarksEqualTo(Constants.BANNER_CHANNEL_APP)*/;
		cmsArticleExample.setOrderByClause("weight desc,update_date desc");
		List<CmsArticle> cmsArticleList = cmsArticleService.selectByExample(cmsArticleExample);
		if (cmsArticleList != null && cmsArticleList.size() > 0) {
			for (CmsArticle cmsArticle : cmsArticleList) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ntcId", cmsArticle.getId());
				map.put("ntctitle", cmsArticle.getTitle());
				map.put("ntcContent", cmsArticle.getDescription() != null ? cmsArticle.getDescription() : "");
//				map.put("ntcImg", cmsArticle.getImage() != null ? wxProperties.getImg_url() + cmsArticle.getImage() : "");
				map.put("ntcImg", cmsArticle.getImage() != null ? cmsArticle.getImage() : "");
				map.put("author", cmsArticle.getKeywords() != null ? cmsArticle.getKeywords() : "");
				map.put("ntcDate", DateUtil.format(cmsArticle.getCreateDate(), DateUtil.FORMAT_LONG));
				map.put("subjectName", subjectName);
				list.add(map);
				CmsArticleDataExample cmsArticleDataExample = new CmsArticleDataExample();
				cmsArticleDataExample.createCriteria().andIdEqualTo(cmsArticle.getId());
				List<CmsArticleData> cmsArticleDataList = cmsArticleDataService.selectByExample(cmsArticleDataExample);
				if (cmsArticleDataList != null && cmsArticleDataList.size() > 0) {
					map.put("ntcContent", cmsArticleDataList.get(0).getContent());
				}
			}
		}
		
		data = ((Map<String, Object>) result.get("data"));
//		data.put("currentPage", currentPage);
		data.put("pageNo", currentPage);
		data.put("pageSize", pageSize);
		data.put("totalPage", page.getPages());
		data.put("noticeList", list);
		return result;
	}
	
	/**
	 * 公告详情
	 * @param request
	 * @return
	 */
	@RequestMapping("/content/info")
	@ResponseBody
	public Map<String, Object> contentInfo(HttpServletRequest request){
		/*Map<String,Object> dataMap = new HashMap<String, Object>();
		Map<String,Object> resMap =MapUtil.getRetMap();
		try {
			String ntcId = request.getParameter("ntc_id");
			if(StringUtils.isBlank(ntcId)){
				log.info("/content/info 参数异常");
				resMap.put("result_code", MapUtil.INTF_ERR_CODE);
				resMap.put("result_info", "参数异常");	
				return resMap;
			}
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ntcId", ntcId);
			
			//公告列表
			Map<String,Object> noticeMap = contentService.getContentInfo(params);
			
			dataMap.put("notice", noticeMap);
			resMap.put("data", dataMap);
			
		} catch (Exception e) {
			log.info(e.getMessage());
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");	
		}
		
		return resMap;*/
		Map<String, Object> result = MapUtil.getRetMap();
		Map<String, Object> data = new HashMap<String, Object>();
		result.put("data", data);
		
		String ntcId = request.getParameter("ntc_id");
		if (StringUtils.isBlank(ntcId)) {
			result.put("result_code", MapUtil.INTF_ERR_CODE_415);
			result.put("result_info", "参数异常");	
			return result;
		}
		
		CmsArticleExample cmsArticleExample = new CmsArticleExample();
		cmsArticleExample.createCriteria().andIdEqualTo(ntcId);
		List<CmsArticle> cmsArticleList = cmsArticleService.selectByExample(cmsArticleExample);
		if (cmsArticleList == null || cmsArticleList.size() == 0) {
			result.put("result_code", MapUtil.INTF_ERR_CODE_415);
			result.put("result_info", "参数异常");	
			return result;
		}
		
		CmsArticle cmsArticle = cmsArticleList.get(0);
		String subjectName = "网站公告";
		if ("3".equals(cmsArticle.getCategoryId())) {
			subjectName = "网站公告";
		}else if ("4".equals(cmsArticle.getCategoryId())) {
			subjectName = "行业动态";
		}else if ("5".equals(cmsArticle.getCategoryId())) {
			subjectName = "帮助中心";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ntcId", cmsArticle.getId());
		map.put("ntctitle", cmsArticle.getTitle());
		map.put("ntcContent", cmsArticle.getDescription() != null ? cmsArticle.getDescription() : "");
//		map.put("ntcImg", cmsArticle.getImage() != null ? wxProperties.getImg_url() + cmsArticle.getImage() : "");
		map.put("ntcImg", cmsArticle.getImage() != null ? cmsArticle.getImage() : "");
		map.put("author", cmsArticle.getKeywords() != null ? cmsArticle.getKeywords() : "");
		map.put("ntcDate", DateUtil.format(cmsArticle.getCreateDate(), DateUtil.FORMAT_LONG));
		map.put("subjectName", subjectName);
		CmsArticleDataExample cmsArticleDataExample = new CmsArticleDataExample();
		cmsArticleDataExample.createCriteria().andIdEqualTo(cmsArticle.getId());
		List<CmsArticleData> cmsArticleDataList = cmsArticleDataService.selectByExample(cmsArticleDataExample);
		if (cmsArticleDataList != null && cmsArticleDataList.size() > 0) {
			map.put("ntcContent", cmsArticleDataList.get(0).getContent());
		}
		
		data = ((Map<String, Object>) result.get("data"));
		data.put("notice", map);
		return result;
	}
}
